package cn.com.cetccst.appstore.ui.fragment.viewmodel;

import android.text.TextUtils;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.com.cetccst.appstore.log.AppStoreLogger;

public abstract class BaseViewModel extends ViewModel {

    public MutableLiveData<String> searchKey = new MutableLiveData<>();
    public MutableLiveData<Boolean> refreshing = new MutableLiveData<>(false);

    protected boolean isValidSearch(String searchKey) {
        if (TextUtils.isEmpty(searchKey)) {
            return true;
        }
        final Pattern SEARCH_PATTERN = Pattern.compile("^[\u4e00-\u9fa5a-zA-Z0-9 ]{1,20}$");
        try {
            Matcher m = SEARCH_PATTERN.matcher(searchKey);
            if (m.find()) {
                return true;
            }
        } catch (Exception ex) {
            AppStoreLogger.info("isValidSearch error: ", ex);
        }
        return false;
    }

    protected void resetSearchResult() {
        refreshing.setValue(false);
    }

    public boolean isSearchMode() {
        return !TextUtils.isEmpty(searchKey.getValue());
    }
}
